﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EM.Components.Business;
using EM.Components.Entities;
using EM.Web.Code;
using System.Security.Principal;

namespace EM.Web.Controllers
{
    public class CampaignController : Controller
    {
        CampaignProvider _service;
        public CampaignController() 
        {
            _service = new CampaignProvider();
        }

        //
        // GET: /Campaign/

        public ActionResult Index()
        {
            var list = new List<EM_Campaigns>();
            if (Request.QueryString.AllKeys.Contains("status"))
            {
                string status = Request.QueryString["status"];
                switch (status.ToLower())
                {
                    case "active":
                        list = _service.GetActive();
                        break;
                    case "draft":
                        list = _service.GetDraft();
                        break;
                    default:
                        list = _service.GetAll();
                        break;
                }
            }
            else 
            {
                list = _service.GetAll();
            }
            return View(list);
        }

        //
        // GET: /Campaign/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Campaign/Create

        public ActionResult Create()
        {
            return View("Edit");
        } 

        //
        // POST: /Campaign/Create

        [HttpPost]
        public ActionResult Create(EM_Campaigns campaign)
        {
            try
            {
                campaign.Owner = WindowsIdentity.GetCurrent().Name;
                campaign.CreatedBy = campaign.Owner;
                campaign.CreatedDate = DateTime.Now;
                campaign.Guid = Guid.NewGuid();
                _service.Add(campaign);

                return RedirectToAction("Edit", "Email", new {id=campaign.CampaignID });
            }
            catch
            {
               // return View();
            }

            return View("Edit",campaign);
        }
        
        //
        // GET: /Campaign/Edit/5
 
        public ActionResult Edit(int id)
        {
            EM_Campaigns entity = _service.Get(id);
            return View(entity);
        }

        //
        // POST: /Campaign/Edit/5

        [HttpPost]
        public ActionResult Edit(EM_Campaigns entityToChange)
        {
            try
            {
                _service.Update(entityToChange);

                return RedirectToAction("Edit", "Email", new {id=entityToChange.CampaignID});
            }
            catch
            {
                return View(entityToChange);
            }
        }

        //
        // GET: /Campaign/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Campaign/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // Get: /Campaign/Approve
        public ActionResult Approve() 
        {
            var list = _service.GetPending();
            return View(list);
        }

        [HttpPost]
        public ActionResult Approve(string[] SelectedObjectIds) 
        {
            if (SelectedObjectIds != null) 
            {
                foreach (var Id in SelectedObjectIds) 
                {
                    _service.Approve(int.Parse(Id), User.Identity.Name);
                }      
            }
            var list = _service.GetPending();
                return View(list);
        }

        //Get: /Campagin/Summary/5
        public ActionResult Summary(int id) 
        {
            EM_Campaigns entity = _service.Get(id);
            return View(entity);
        }

        [HttpPost]
        public ActionResult Summary(string button) 
        {
            // Submit for approval
           
            int id = int.Parse(button);
            EM_Campaigns entity = _service.Get(id);
            entity.Submitted = true;
            _service.Update(entity);
            return RedirectToAction("Index");
        }
    }
}
